package utils
{
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.utils.getTimer;

public class TimeUtil
{    
    public static const HOUR_24_SEC:Number = 86400;   
    
	public static const TIME_SEP:String = ":";
	
	private static var currentTime:Number = 0; //sec
	
	private static var date:Date = new Date();
	
	private static var timer:Timer;
	
	private static var lastTime:Number;
	
	public static function get currentGameTime():Date
	{
		return date;
	}
	
	public static function get currentGameTimeSec():Number
	{
		return currentGameTime.time / 1000;
	}
	
	public static function recordeTime():Number
	{
		currentTime = getTimer() / 1000;
		return currentTime;
	}
	
	public static function getDurationFromLastRecorde():Number
	{
		return getTimer()/1000 - currentTime;
	}
	
	public static function currentStandardTime():Number
	{
		var date:Date = new Date();
		return date.time;
	}
	
	public static function parseMinsToString(value:Number):String
	{
		var h:Number = Math.floor(value / 60);
		var hStr:String = "";
		if (h < 10)
		{
			hStr = "0" + h.toString();
		}
		else
		{
			hStr = h.toString();
		}
		
		var s:Number = Math.floor(value % 60);
        var sStr:String = "";
        if (s < 10)
        {
            sStr = "0" + s.toString();
        }
        else
        {
            sStr = s.toString();
        }
		return hStr + TIME_SEP + sStr;
	}
    
    public static function parseSecsToString(value:Number):String
    {
        var m:Number = Math.floor(value / 60);
		
		var h:Number = Math.floor(m / 60);
		h = Math.max(0, h);
		var hStr:String = "";
		if (h < 10)
		{
			hStr = "0" + h.toString();
		}
		else
		{
			hStr = h.toString();
		}
		
		m = Math.floor(m % 60);
		m = Math.max(0, m);
		var mStr:String = "";
		if (m < 10)
		{
			mStr = "0" + m.toString();
		}
		else
		{
			mStr = m.toString();
		}
        
        var s:Number = Math.floor(value % 60);
		s = Math.max(0, s);
        var sStr:String = "";
        if (s < 10)
        {
            sStr = "0" + s.toString();
        }
        else
        {
            sStr = s.toString();
        }
        return hStr + TIME_SEP + mStr + TIME_SEP + sStr;
    }
	
	public static function startGameTime(timeStr:String):void
	{
		date.time = Number(timeStr) * 1000;
		
		lastTime = getTimer();
        
		timer = new Timer(500);
		timer.addEventListener(TimerEvent.TIMER, timeRunning);
		timer.start();
	}
	
	private static function timeRunning(event:Event):void
	{
		var old:Number = lastTime;
		lastTime = getTimer(); 
		date.time += (lastTime - old);
		
		for each ( var func:Function in renderFunctions ) 
		{
			func();
		}
	}
	
	/**
	 * startTime -> 计时开始的时间(迄今为止的秒数);   time -> 时间 (秒数)
	 * @param startTime
	 * @param time
	 * @return true -> 到时
	 *         false -> 还没到时
	 * 
	 */	
	public static function isTimeout(startTime:Number, time:Number):Boolean
	{
		if ((currentGameTimeSec - startTime) >= time)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	private static var renderFunctions : Vector.<Function> = new Vector.<Function> ();
	
	public static function addRenderFunction ( func : Function ) : void 
	{		
		if (func == null || renderFunctions.indexOf( func) >= 0) return;
		renderFunctions.push( func );		
	}
	
	public static function removeRenderFunction ( func : Function ) : void 
	{
		var index:int = renderFunctions.indexOf( func);
		if (index < 0) return;
		renderFunctions.splice( index, 1 );		
	}

}
}




